package org.u2ml.app.business.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.u2ml.app.business.ProjectService;
import org.u2ml.app.domain.Project;
import org.u2ml.app.domain.User;

@Component
@Transactional
public class ProjectServiceImpl implements ProjectService {

	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	public List<Project> get() {
		List<Project> list = sessionFactory.getCurrentSession()
    	.createQuery("from Project")
    	.list();
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public Project find(Long oid){
		List<Project> list = sessionFactory.getCurrentSession()
        	.createQuery("from Project project where project.oid = :oid")
        	.setParameter("oid",oid)
        	.list();
		return (list.size()==0) ? null : list.get(0);
	}

	@SuppressWarnings("unchecked")
	public Project findByName(String name) {
		List<Project> list = sessionFactory.getCurrentSession()
    	.createQuery("from Project project where project.name = :name")
    	.setParameter("name",name)
    	.list();
	return (list.size()==0) ? null : list.get(0);
	}

	public Project save(Project project) {
		sessionFactory.getCurrentSession().save(project);
		return project;
	}

	public Project update(Project project) {
		sessionFactory.getCurrentSession().update(project);
		return project;
	}
	
	public void delete(Project project) {
		sessionFactory.getCurrentSession().delete(project);
	}
	
	@SuppressWarnings("unchecked")
	public boolean isContributor(Long oid,Long userId) {
		Session session = sessionFactory.getCurrentSession();
		List<User> userList = session.createQuery("from User user where user.oid = :oid").setParameter("oid",userId).list();
		if (userList.size()==0)
			return false;
		User u = userList.get(0);
		List<Project> projectList = session.createQuery("from Project project where project.oid = :oid").setParameter("oid",oid).list();
		if (projectList.size()==0)
			return false;
		Project project = projectList.get(0);
		return project.getContributors().contains(u);
	}
	
	@SuppressWarnings("unchecked")
	public void addContributor(Long oid, Long userId) {
		Session session = sessionFactory.getCurrentSession();
		List<User> userList = session.createQuery("from User user where user.oid = :oid").setParameter("oid",userId).list();
		if (userList.size()==0)
			return;
		User user = userList.get(0);
		List<Project> projectList = session.createQuery("from Project project where project.oid = :oid").setParameter("oid",oid).list();
		if (projectList.size()==0)
			return;
		Project project = projectList.get(0);
		project.addContributor(user);
	}
	
	@SuppressWarnings("unchecked")
	public void removeContributor(Long oid, Long userId) {
		Session session = sessionFactory.getCurrentSession();
		List<User> userList = 
			session.createQuery("from User user where user.oid = :oid")
			.setParameter("oid", userId)
			.list();
		if (userList.size() == 0)
			return;
		User user = userList.get(0);
		List<Project> projectList = 
			session.createQuery("from Project project where project.oid = :oid")
			.setParameter("oid", oid)
			.list();
		if (projectList.size() == 0)
			return;
		Project project = projectList.get(0);
		project.removeContributor(user);
	}
}
